package com.dingreading.cloud.admin.controller;

import com.aliyuncs.auth.sts.AssumeRoleResponse;
import com.dingreading.cloud.admin.util.CdnUrlUtil;
import com.dingreading.cloud.admin.util.WaterMarkUtil;
import com.dingreading.cloud.admin.util.redis.RedisService;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.FileUtil;
import com.dingreading.cloud.common.util.aliyun.GetObjectProgressListener;
import com.dingreading.cloud.common.util.aliyun.OSSClientUtil;
import com.dingreading.cloud.common.util.aliyun.OssStsDto;
import com.dingreading.cloud.common.util.aliyun.ProgressDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@RestController
@RequestMapping("/system/upload")
@Api(tags = "阿里云-上传文件")
public class OssUploadController {

    @Resource
    private CdnUrlUtil cdnUrlUtil;
    @Resource
    private RedisService redisService;

    @Value("${waterImgUrl}")
    private String waterImgUrl;

    // 文件存储目录-总部端门店跟进记录图片
    public static final String storeStoreFollowImgDir = "store/followImg/";
    // 文件存储目录-合同目录
    public static final String contractDir = "contract/";
    // 文件存储目录-备课课次图片目录
    public static final String subjectLessonDir = "subjectLesson/";


    @ApiOperation(value = "学院课程视频上传")
    @PostMapping(value = "/courseData", produces = "application/json; charset=UTF-8")
    public R<Object> courseData(
            @ApiParam(value = "文件") @RequestPart(value = "file") MultipartFile file
    ) {
        boolean m = FileUtil.checkFileSize(file.getSize(), 1024, "M");
        if (!m) return R.fail("上传文件大小不能超过1024M");

        OSSClientUtil ossClientUtil = new OSSClientUtil();

        String securityToken = (String) redisService.get(OSSClientUtil.STS_SECURITYTOKEN);
        String accessKeyId = (String) redisService.get(OSSClientUtil.STS_ACCESSKEYID);
        String accessKeySecret = (String) redisService.get(OSSClientUtil.STS_ACCESSKEYSECRET);
        if (StringUtils.isBlank(securityToken) || StringUtils.isBlank(accessKeyId) || StringUtils.isBlank(accessKeySecret)) {
            AssumeRoleResponse.Credentials response = ossClientUtil.getAssumeRoleResponse();
            if (response == null) return R.fail("获取阿里云STS授权失败");

            securityToken = response.getSecurityToken();
            accessKeyId = response.getAccessKeyId();
            accessKeySecret = response.getAccessKeySecret();

            redisService.set(OSSClientUtil.STS_SECURITYTOKEN, securityToken, 1800);
            redisService.set(OSSClientUtil.STS_ACCESSKEYID, accessKeyId, 1800);
            redisService.set(OSSClientUtil.STS_ACCESSKEYSECRET, accessKeySecret, 1800);
        }

        String todayDir = ossClientUtil.getTodaySplitDir(OSSClientUtil.courseDataDir);
        if (StringUtils.isBlank(todayDir)) return R.fail("获取阿里云上传路径失败");

        String fileName = ossClientUtil.getFileName(file);
        if (StringUtils.isBlank(fileName)) return R.fail("获取阿里云文件名失败");

        String taskId = UUID.randomUUID().toString();
        GetObjectProgressListener progressListener = null;
        try {
            progressListener = new GetObjectProgressListener(taskId);
            ossClientUtil.uploadStsFile(OSSClientUtil.privateBucket_subject, securityToken, accessKeyId, accessKeySecret,
                    file.getInputStream(), todayDir, fileName, progressListener);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ProgressDto progressDto = new ProgressDto();
        progressDto.setUrl(todayDir + fileName);
        progressDto.setTaskId(taskId);
        return R.ok(progressDto);
    }

    @ApiOperation(value = "上传图片")
    @PostMapping(value = "/image", produces = "application/json; charset=UTF-8")
    public R<Object> image(
            @ApiParam(value = "图片文件") @RequestPart(value = "image") MultipartFile multipartFile,
            @ApiParam(value = "平台类型 1.管理 2.加盟商 3.移动端 4.合同 5.商城") @RequestParam(value = "type") Integer type
    ) {
        boolean m = FileUtil.checkFileSize(multipartFile.getSize(), 5, "M");
        if (!m) return R.fail("上传图片大小不能超过5M");

        OSSClientUtil ossClientUtil = new OSSClientUtil();
        ossClientUtil.initOss();

        String todayDir = "";

        if (type.equals(1)) {
            todayDir = ossClientUtil.getTodaySplitDir(OSSClientUtil.systemDir);
        } else if (type.equals(2)) {
            todayDir = ossClientUtil.getTodaySplitDir(OSSClientUtil.storeDir);
        } else if (type.equals(3)) {
            todayDir = ossClientUtil.getTodaySplitDir(OSSClientUtil.mobileDir);
        } else if (type.equals(4)) {
            todayDir = ossClientUtil.getTodaySplitDir(OSSClientUtil.contractDir);
        } else if (type.equals(5)) {
            todayDir = ossClientUtil.getTodaySplitDir(OSSClientUtil.mallDir);
        } else {
            return R.fail("平台类型错误");
        }

        String resultUrl = ossClientUtil.uploadImg2Oss(todayDir, multipartFile);
        ossClientUtil.destory();
        if (StringUtils.isBlank(resultUrl)) {
            return R.fail("上传图片失败");
        }
        return R.ok(todayDir + resultUrl);
    }

    @ApiOperation(value = "通过任务的taskId获取上传/下载进度")
    @PostMapping(value = "/getUploadProgress", produces = "application/json; charset=UTF-8")
    public R<Object> getUploadProgress(
            @ApiParam(value = "上传的任务id") @RequestParam(value = "taskId") String taskId
    ) {
        GetObjectProgressListener progressListener = GetObjectProgressListener.progressBarMap.get(taskId);
        if (progressListener != null) {
            int percent = progressListener.getPercent();
            int succeed = progressListener.getSucceed();
            ProgressDto progressDto = new ProgressDto();
            progressDto.setPercent(percent);
            progressDto.setSuccess(succeed);
            if (succeed != 0) {
                // 完成或失败，移除任务
                GetObjectProgressListener.progressBarMap.remove(taskId);
            }
            return R.ok(progressDto);
        }
        return R.ok();
    }

    @ApiOperation(value = "获取叮叮学院上传阿里云的临时授权信息")
    @PostMapping(value = "/getCourseOssCredit", produces = "application/json; charset=UTF-8")
    public R<Object> getCourseOssCredit(
            HttpServletRequest request,
            @ApiParam(value = "文件类型") @RequestParam(value = "fileType") String fileType
    ) {
        OSSClientUtil ossClientUtil = new OSSClientUtil();
        String todayDir = ossClientUtil.getTodaySplitDir(OSSClientUtil.courseDataDir);
        if (StringUtils.isBlank(todayDir)) return R.fail("获取阿里云上传路径失败");

        String fileName = ossClientUtil.newFileName(fileType);
        if (StringUtils.isBlank(fileName)) return R.fail("生成文件名失败");

        String securityToken = (String) redisService.get(OSSClientUtil.STS_SECURITYTOKEN);
        String accessKeyId = (String) redisService.get(OSSClientUtil.STS_ACCESSKEYID);
        String accessKeySecret = (String) redisService.get(OSSClientUtil.STS_ACCESSKEYSECRET);
        if (StringUtils.isBlank(securityToken) || StringUtils.isBlank(accessKeyId) || StringUtils.isBlank(accessKeySecret)) {
            AssumeRoleResponse.Credentials response = ossClientUtil.getAssumeRoleResponse();
            if (response == null) return R.fail("获取阿里云STS授权失败");

            securityToken = response.getSecurityToken();
            accessKeyId = response.getAccessKeyId();
            accessKeySecret = response.getAccessKeySecret();

            redisService.set(OSSClientUtil.STS_SECURITYTOKEN, securityToken, 1800);
            redisService.set(OSSClientUtil.STS_ACCESSKEYID, accessKeyId, 1800);
            redisService.set(OSSClientUtil.STS_ACCESSKEYSECRET, accessKeySecret, 1800);
        }
        String region = OSSClientUtil.region;
        String privateBucket = OSSClientUtil.privateBucket_subject;

        String fileUrl = todayDir.concat(fileName);
        R<String> cdnUrl = cdnUrlUtil.getCdnUrl(request, fileUrl);
        if (R.isError(cdnUrl))
            return R.fail("获取CDN地址失败");
        String cdnUrlStr = cdnUrl.getData();
        OssStsDto dto = new OssStsDto();
        dto.setAccessKeyId(accessKeyId);
        dto.setAccessKeySecret(accessKeySecret);
        dto.setSecurityToken(securityToken);
        dto.setRegion(region);
        dto.setBucket(privateBucket);
        dto.setFileName(fileUrl);
        dto.setCdnUrl(cdnUrlStr);
        return R.ok(dto);
    }

    @ApiOperation(value = "商城商品详情上传图片并添加水印图")
    @PostMapping("/goodsImgAddWater")
    public R<Object> goodsImgAddWater(
            @ApiParam(value = "图片文件") @RequestPart(value = "image") MultipartFile multipartFile
    ) {
        if (multipartFile == null) return R.fail("图片文件不能为空");

        File waterImgFile = new File(waterImgUrl);
        if (!waterImgFile.exists())
            return R.fail("未发现水印图片");

        String fileName = multipartFile.getOriginalFilename();
        File file = WaterMarkUtil.MultipartFileToFile(multipartFile);

        InputStream inputStream = WaterMarkUtil.markImageByIconToStream(file, waterImgUrl);
        if (inputStream == null) return R.fail("图片添加水印失败");

        // 图片添加水印并上传oss
        OSSClientUtil ossClientUtil = new OSSClientUtil();
        ossClientUtil.initOss();
        String todayDir = ossClientUtil.getTodaySplitDir(OSSClientUtil.mallDir);
        String resultUrl = ossClientUtil.uploadImg2Oss(todayDir, inputStream, fileName);
        ossClientUtil.destory();
        if (StringUtils.isBlank(resultUrl)) {
            return R.fail("上传图片失败");
        }
        return R.ok(todayDir + resultUrl);
    }

    @ApiOperation(value = "上传合同文件")
    @PostMapping(value = "/contractsFile", produces = "application/json; charset=UTF-8")
    public R<Object> contractsFile(
            @ApiParam(value = "文件") @RequestPart(value = "file") MultipartFile file
    ) {
        boolean m = FileUtil.checkFileSize(file.getSize(), 50, "M");
        if (!m) return R.fail("上传文件大小不能超过50M");

        OSSClientUtil ossClientUtil = new OSSClientUtil();

        String securityToken = (String) redisService.get(OSSClientUtil.STS_SECURITYTOKEN);
        String accessKeyId = (String) redisService.get(OSSClientUtil.STS_ACCESSKEYID);
        String accessKeySecret = (String) redisService.get(OSSClientUtil.STS_ACCESSKEYSECRET);
        if (StringUtils.isBlank(securityToken) || StringUtils.isBlank(accessKeyId) || StringUtils.isBlank(accessKeySecret)) {
            AssumeRoleResponse.Credentials response = ossClientUtil.getAssumeRoleResponse();
            if (response == null) return R.fail("获取阿里云STS授权失败");

            securityToken = response.getSecurityToken();
            accessKeyId = response.getAccessKeyId();
            accessKeySecret = response.getAccessKeySecret();

            redisService.set(OSSClientUtil.STS_SECURITYTOKEN, securityToken, 1800);
            redisService.set(OSSClientUtil.STS_ACCESSKEYID, accessKeyId, 1800);
            redisService.set(OSSClientUtil.STS_ACCESSKEYSECRET, accessKeySecret, 1800);
        }

        String todayDir = ossClientUtil.getTodaySplitDir(OSSClientUtil.contractDir);
        if (StringUtils.isBlank(todayDir)) return R.fail("获取阿里云上传路径失败");

        String fileName = ossClientUtil.getFileName(file);
        if (StringUtils.isBlank(fileName)) return R.fail("获取阿里云文件名失败");

//        String resultUrl = ossClientUtil.uploadImg2Oss(todayDir, multipartFile);
//        ossClientUtil.destory();
//        if (StringUtils.isBlank(resultUrl)){
//            return R.fail("上传合同文件失败");
//        }
//        return R.ok(todayDir + resultUrl);

        String taskId = UUID.randomUUID().toString();
        GetObjectProgressListener progressListener = null;
        try {
            progressListener = new GetObjectProgressListener(taskId);
            ossClientUtil.uploadStsFile(OSSClientUtil.publicBucket, securityToken, accessKeyId, accessKeySecret,
                    file.getInputStream(), todayDir, fileName, progressListener);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ProgressDto progressDto = new ProgressDto();
        progressDto.setUrl(todayDir + fileName);
        progressDto.setTaskId(taskId);
        return R.ok(progressDto);
    }

    @ApiOperation(value = "获取上传公共bucket的临时授权信息")
    @PostMapping(value = "/getContractOssCredit", produces = "application/json; charset=UTF-8")
    public R<Object> getContractOssCredit(
            @ApiParam(value = "文件类型") @RequestParam(value = "fileType") String fileType
    ) {
        OSSClientUtil ossClientUtil = new OSSClientUtil();
        String todayDir = ossClientUtil.getTodaySplitDir(OSSClientUtil.contractDir);
        if (StringUtils.isBlank(todayDir)) return R.fail("获取阿里云上传路径失败");

        String fileName = ossClientUtil.newFileName(fileType);
        if (StringUtils.isBlank(fileName)) return R.fail("生成文件名失败");

        String securityToken = (String) redisService.get(OSSClientUtil.STS_SECURITYTOKEN);
        String accessKeyId = (String) redisService.get(OSSClientUtil.STS_ACCESSKEYID);
        String accessKeySecret = (String) redisService.get(OSSClientUtil.STS_ACCESSKEYSECRET);
        if (StringUtils.isBlank(securityToken) || StringUtils.isBlank(accessKeyId) || StringUtils.isBlank(accessKeySecret)) {
            AssumeRoleResponse.Credentials response = ossClientUtil.getAssumeRoleResponse();
            if (response == null) return R.fail("获取阿里云STS授权失败");

            securityToken = response.getSecurityToken();
            accessKeyId = response.getAccessKeyId();
            accessKeySecret = response.getAccessKeySecret();

            redisService.set(OSSClientUtil.STS_SECURITYTOKEN, securityToken, 1800);
            redisService.set(OSSClientUtil.STS_ACCESSKEYID, accessKeyId, 1800);
            redisService.set(OSSClientUtil.STS_ACCESSKEYSECRET, accessKeySecret, 1800);
        }
        String region = OSSClientUtil.region;
        String privateBucket = OSSClientUtil.publicBucket;

        OssStsDto dto = new OssStsDto();
        dto.setAccessKeyId(accessKeyId);
        dto.setAccessKeySecret(accessKeySecret);
        dto.setSecurityToken(securityToken);
        dto.setRegion(region);
        dto.setBucket(privateBucket);
        dto.setFileName(todayDir.concat(fileName));
        return R.ok(dto);
    }

    @ApiOperation(value = "门店-获取上传公共bucket的临时授权信息")
    @PostMapping(value = "/getPubBucketOssCredit", produces = "application/json; charset=UTF-8")
    public R<Object> getPubBucketOssCredit(
            @ApiParam(value = "文件的类型") @RequestParam(value = "fileType") String fileType,
            @ApiParam(value = "上传的文件目录：付款截图:paypic") @RequestParam(value = "fileDir") String fileDir
    ) {
        OSSClientUtil ossClientUtil = new OSSClientUtil();
        // 获取上传路径
        String todayDir = "";
        if (fileDir.equals("paypic")) {
            todayDir = ossClientUtil.getTodaySplitDir(OSSClientUtil.paypicDir);
        } else if (fileDir.equals("store/followImg/")) {
            todayDir = ossClientUtil.getTodaySplitDir(storeStoreFollowImgDir);
        } else if (fileDir.equals("contract/")) {
            todayDir = ossClientUtil.getTodaySplitDir(contractDir);
        } else if (fileDir.equals("subjectLesson" +
                "")) {
            todayDir = ossClientUtil.getTodaySplitDir(subjectLessonDir);
        } else {
            return R.fail("上传的文件目录不存在");
        }
        if (StringUtils.isBlank(todayDir))
            return R.fail("获取阿里云上传路径失败");

        // 获取 随机名称.文件类型
        String fileName = ossClientUtil.newFileName(fileType);
        if (StringUtils.isBlank(fileName))
            return R.fail("生成文件名失败");

        // 获取redis里的临时授权信息
        String securityToken = (String) redisService.get(OSSClientUtil.STS_SECURITYTOKEN);
        String accessKeyId = (String) redisService.get(OSSClientUtil.STS_ACCESSKEYID);
        String accessKeySecret = (String) redisService.get(OSSClientUtil.STS_ACCESSKEYSECRET);
        if (StringUtils.isBlank(securityToken) || StringUtils.isBlank(accessKeyId) || StringUtils.isBlank(accessKeySecret)) {
            // 重新获取阿里云的临时授权信息
            AssumeRoleResponse.Credentials response = ossClientUtil.getAssumeRoleResponse();
            if (response == null)
                return R.fail("获取阿里云STS授权失败");

            securityToken = response.getSecurityToken();
            accessKeyId = response.getAccessKeyId();
            accessKeySecret = response.getAccessKeySecret();

            redisService.set(OSSClientUtil.STS_SECURITYTOKEN, securityToken, 1800);
            redisService.set(OSSClientUtil.STS_ACCESSKEYID, accessKeyId, 1800);
            redisService.set(OSSClientUtil.STS_ACCESSKEYSECRET, accessKeySecret, 1800);
        }
        String region = OSSClientUtil.region;
        String privateBucket = OSSClientUtil.publicBucket;

        OssStsDto dto = new OssStsDto();
        dto.setAccessKeyId(accessKeyId);
        dto.setAccessKeySecret(accessKeySecret);
        dto.setSecurityToken(securityToken);
        dto.setRegion(region);
        dto.setBucket(privateBucket);
        dto.setFileName(todayDir.concat(fileName));
        return R.ok(dto);
    }


    @ApiOperation(value = "获取习作视频上传阿里云的临时授权信息")
    @PostMapping(value = "/getStudyVideoOssCredit", produces = "application/json; charset=UTF-8")
    public R<Object> getStudyVideoOssCredit(
            HttpServletRequest request,
            @ApiParam(value = "文件类型") @RequestParam(value = "fileType") String fileType
    ) {
        OSSClientUtil ossClientUtil = new OSSClientUtil();
        String todayDir = ossClientUtil.getTodaySplitDir("studyVideo/");
        if (StringUtils.isBlank(todayDir)) return R.fail("获取阿里云上传路径失败");

        String fileName = ossClientUtil.newFileName(fileType);
        if (StringUtils.isBlank(fileName)) return R.fail("生成文件名失败");

        String securityToken = (String) redisService.get(OSSClientUtil.STS_SECURITYTOKEN);
        String accessKeyId = (String) redisService.get(OSSClientUtil.STS_ACCESSKEYID);
        String accessKeySecret = (String) redisService.get(OSSClientUtil.STS_ACCESSKEYSECRET);
        if (StringUtils.isBlank(securityToken) || StringUtils.isBlank(accessKeyId) || StringUtils.isBlank(accessKeySecret)) {
            AssumeRoleResponse.Credentials response = ossClientUtil.getAssumeRoleResponse();
            if (response == null) return R.fail("获取阿里云STS授权失败");

            securityToken = response.getSecurityToken();
            accessKeyId = response.getAccessKeyId();
            accessKeySecret = response.getAccessKeySecret();

            redisService.set(OSSClientUtil.STS_SECURITYTOKEN, securityToken, 1800);
            redisService.set(OSSClientUtil.STS_ACCESSKEYID, accessKeyId, 1800);
            redisService.set(OSSClientUtil.STS_ACCESSKEYSECRET, accessKeySecret, 1800);
        }
        String region = OSSClientUtil.region;
        String privateBucket = OSSClientUtil.privateBucket_subject;

        OssStsDto dto = new OssStsDto();
        dto.setAccessKeyId(accessKeyId);
        dto.setAccessKeySecret(accessKeySecret);
        dto.setSecurityToken(securityToken);
        dto.setRegion(region);
        dto.setBucket(privateBucket);
        dto.setFileName(todayDir.concat(fileName));
        if (StringUtils.isNotBlank(dto.getFileName())) {
            R<String> r = cdnUrlUtil.getCdnUrl(request, dto.getFileName());
            if (R.isError(r))
                return R.fail(r.getMsg());
            dto.setCdnUrl(r.getData());
        }
        return R.ok(dto);
    }


    @ApiOperation(value = "获取课次视频和图片上传阿里云的临时授权信息")
    @PostMapping(value = "/getSubjectLessonOssCredit", produces = "application/json; charset=UTF-8")
    public R<Object> getSubjectLessonOssCredit(
            HttpServletRequest request,
            @ApiParam(value = "文件类型") @RequestParam(value = "fileType") String fileType
    ) {
        OSSClientUtil ossClientUtil = new OSSClientUtil();
        String todayDir = ossClientUtil.getTodaySplitDir("lessonData/");
        if (StringUtils.isBlank(todayDir)) return R.fail("获取阿里云上传路径失败");

        String fileName = ossClientUtil.newFileName(fileType);
        if (StringUtils.isBlank(fileName)) return R.fail("生成文件名失败");

        String securityToken = (String) redisService.get(OSSClientUtil.STS_SECURITYTOKEN);
        String accessKeyId = (String) redisService.get(OSSClientUtil.STS_ACCESSKEYID);
        String accessKeySecret = (String) redisService.get(OSSClientUtil.STS_ACCESSKEYSECRET);
        if (StringUtils.isBlank(securityToken) || StringUtils.isBlank(accessKeyId) || StringUtils.isBlank(accessKeySecret)) {
            AssumeRoleResponse.Credentials response = ossClientUtil.getAssumeRoleResponse();
            if (response == null) return R.fail("获取阿里云STS授权失败");

            securityToken = response.getSecurityToken();
            accessKeyId = response.getAccessKeyId();
            accessKeySecret = response.getAccessKeySecret();

            redisService.set(OSSClientUtil.STS_SECURITYTOKEN, securityToken, 1800);
            redisService.set(OSSClientUtil.STS_ACCESSKEYID, accessKeyId, 1800);
            redisService.set(OSSClientUtil.STS_ACCESSKEYSECRET, accessKeySecret, 1800);
        }
        String region = OSSClientUtil.region;
        String privateBucket = OSSClientUtil.privateBucket_subject;

        OssStsDto dto = new OssStsDto();
        dto.setAccessKeyId(accessKeyId);
        dto.setAccessKeySecret(accessKeySecret);
        dto.setSecurityToken(securityToken);
        dto.setRegion(region);
        dto.setBucket(privateBucket);
        dto.setFileName(todayDir.concat(fileName));
        if (StringUtils.isNotBlank(dto.getFileName())) {
            R<String> r = cdnUrlUtil.getCdnUrl(request, dto.getFileName());
            if (R.isError(r))
                return R.fail(r.getMsg());
            dto.setCdnUrl(r.getData());
        }
        return R.ok(dto);
    }

//    @ApiOperation(value = "门店-获取上传公共bucket的临时授权信息(好读活动上传视频)")
//    @PostMapping(value = "/getXcxBucketOssCredit", produces = "application/json; charset=UTF-8")
//    public R<Object> getXcxBucketOssCredit() {
//
//
//        // 验证能否打卡
//        ReadingDate readingDate = readingDateService.getById(1L);
//        String msg = judgePunch(readingDate);
//        if (StringUtils.isNotBlank(msg))
//            return R.fail(msg);
//
//        OSSClientUtil ossClientUtil = new OSSClientUtil();
//        // 获取上传路径
//        String todayDir = ossClientUtil.getTodaySplitDir(OSSClientUtil.xcxMdbReadingDir);
//
//        if (StringUtils.isBlank(todayDir))
//            return R.fail("获取阿里云上传路径失败");
//
//        // 获取 随机名称.文件类型
//        String fileName = KitUtil.getUuid() + ".mp4";
//        if (StringUtils.isBlank(fileName))
//            return R.fail("生成文件名失败");
//
//        // 获取redis里的临时授权信息
//        String securityToken = (String) redisService.get(OSSClientUtil.STS_SECURITYTOKEN);
//        String accessKeyId = (String) redisService.get(OSSClientUtil.STS_ACCESSKEYID);
//        String accessKeySecret = (String) redisService.get(OSSClientUtil.STS_ACCESSKEYSECRET);
//        String expiration = (String) redisService.get(OSSClientUtil.STS_EXPIRATION);
//        if (StringUtils.isBlank(securityToken) || StringUtils.isBlank(accessKeyId) || StringUtils.isBlank(accessKeySecret)) {
//            // 重新获取阿里云的临时授权信息
//            AssumeRoleResponse.Credentials response = ossClientUtil.getAssumeRoleResponse();
//            if (response == null) return R.fail("获取阿里云STS授权失败");
//
//            securityToken = response.getSecurityToken();
//            accessKeyId = response.getAccessKeyId();
//            accessKeySecret = response.getAccessKeySecret();
//            expiration = response.getExpiration();
//        }
//        String region = OSSClientUtil.region;
//        String publicBucket = OSSClientUtil.publicBucket;
//
//        OssStsDto dto = new OssStsDto();
//        dto.setAccessKeyId(accessKeyId);
//        dto.setAccessKeySecret(accessKeySecret);
//        dto.setSecurityToken(securityToken);
//        dto.setRegion(region);
//        dto.setBucket(publicBucket);
//        dto.setFileName(todayDir.concat(fileName));
//        dto.setExpiration(expiration);
//        return R.ok(dto);
//    }

    /**
     * 验证能否打卡
     */
//    private String judgePunch(ReadingDate readingDate) {
//        if (readingDate == null)
//            return "活动日期获取失败";
//        String beginDate = readingDate.getPunchBeginDate();
//        String endDate = readingDate.getPunchEndDate();
//        if (StringUtils.isBlank(beginDate) || StringUtils.isBlank(endDate))
//            return "打卡日期获取失败";
//        // 判读活动开始时间和结束时间
//        if (judgeDateOver(beginDate, endDate))
//            return "打卡日期:" + beginDate + "至" + endDate;
//        return "";
//    }

    /**
     * 验证开始和结束日期
     */
//    private boolean judgeDateOver(String beginDate, String endDate) {
//        try {
//            Date bgnDate = DateUtil.parseDate(beginDate);
//            Date enDate = DateUtil.parseDate(endDate);
//            String today = DateUtil.getNowDateStr();
//            Date todayDate = DateUtil.parseDate(today);
//            if (todayDate.getTime() < bgnDate.getTime() || todayDate.getTime() > enDate.getTime()) {
//                return true;
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return false;
//    }

}
